﻿// open new window
function AnhXuanOpenNewWindow(link)
{
    var winSize = ",width=" + (screen.availWidth).toString() + ",height=" + (screen.availHeight).toString();

    var ControlBar="fullscreen=no,menubar=no, toolbar=no, scrollbars=yes,top=0,left=0, titlebar=no, resizable=yes,status=no,location=no ";
    window.open(link,'',ControlBar+winSize);
}
function AnhXuanOpenSetSize(link,width,height)
{
    var winSize = ",width=" + width.toString() + ",height=" + height.toString();

    var ControlBar="fullscreen=no,menubar=no, toolbar=no, scrollbars=yes,top=0,left=0, titlebar=no, resizable=yes,status=no,location=no ";
    window.open(link,'',ControlBar+winSize);
}
function toggleDisplay(blockId, titleId)
{
    
	var block = document.getElementById(blockId);
	var title = document.getElementById(titleId);
	if (block.className.indexOf("qsfNone") == -1)
	{
		block.className = block.className + " qsfNone";
		title.className = "qsfSubtitleCollapsed";
	}
	else
	{
		block.className = block.className.replace(/qsfNone/, "");
		title.className = "qsfSubtitle";
	}
	
	return false;
	 
}
function toggleListLab(blockId, titleId)
{
    
	var block = document.getElementById(blockId);
	var title = document.getElementById(titleId);
	if (block.className.indexOf("qsfNone") == -1)
	{
		block.className = "qsfNone";
		title.className = "qsfSubtitleCollapsed";
	}
	else
	{
		block.className = block.className.replace(/qsfNone/, "");
		title.className = "qsfSubtitle";
	}	
	return false;
	 
}
function labListToShowAll(hiddenContainsListId)
{
    var lstId=document.getElementById(hiddenContainsListId);
    var arrId=lstId.value.split(';');    
    for(i=0;i<arrId.length;i++)
    {
        var divToShow=document.getElementById(arrId[i]);
        divToShow.className="";        
    }  
}
function labListToHidewAll(hiddenContainsListId)
{
    var lstId=document.getElementById(hiddenContainsListId);
    var arrId=lstId.value.split(';');    
    for(i=0;i<arrId.length;i++)
    {
        var divToShow=document.getElementById(arrId[i]);
        divToShow.className="qsfNone";        
    }
   
    
}



 var hasChanges, inputs, dropdowns, editedRow;
                                    
function KeyPress(sender, args) {  
     if (args.get_keyCharacter() == sender.get_numberFormat().DecimalSeparator) {
         args.set_cancel(true);  
      }  
}  
function hov(loc,cls) 
{ 
	if(loc.className) 
		loc.className=cls; 
}

/*------Kiểm tra số nguyên dương--------------------------------------- */
function NumberFilter(e)
{          /*Grabbing the unicode value of the key that was pressed*/
	 var unicode;
    if(window.event)
              unicode = window.event.keyCode;     //IE
    else
          unicode = e.which;     //firefox
          
    /* tab null backspace , minus*/
      if(unicode == 9 || unicode == 8 || unicode == 0 )
        {
            return true;
        }
        
    /*if the value entered is not a unicode value between 48 and 57 return false*/
    if(unicode < 48 || unicode > 57)
    {  
        return false;
    } 
    return true;
}

/*------Kiểm tra số nguyên âm--------------------------------------- */
function NumberFilter(e)
{          /*Grabbing the unicode value of the key that was pressed*/
	 var unicode;
    if(window.event)
              unicode = window.event.keyCode;     //IE
    else
          unicode = e.which;     //firefox
          
     /* tab null backspace , minus*/
      if(unicode == 9 || unicode == 8 || unicode == 0 || unicode == 45 || unicode == 189)
        {
            return true;
        }
             
    /*if the value entered is not a unicode value between 48 and 57 return false*/
    if(unicode < 48 || unicode > 57)
    {  
        return false;
    } 
    return true;
}
/*------End kiểm tra số nguyên--------------------------------------- */

/*------Kiểm tra số số thực dấu chấm (.)--------------------------------------- */
function DblFilter(e)
{          /*Grabbing the unicode value of the key that was pressed*/
	 var unicode;
    if(window.event)
          unicode = window.event.keyCode;     //IE
    else
      unicode = e.which;     //firefox
      /* tab null backspace , minus*/
      if(unicode == 9 || unicode == 8 || unicode == 0 || unicode == 45 || unicode == 189)
        {
            return true;
        }
     /*alert(unicode);*/
    /*if the value entered is not a unicode value between 48 and 57 return false - permit comma*/
    
    if(unicode == 44)
    {
        return true;
    }
    else
    {
        if(unicode < 48 || unicode > 57)
        {  
            return false;
        } 
    }
    return true;
}
/*------End kiểm tra số nguyên--------------------------------------- */

/*------Kiểm tra số số thực dấu phẩy (,)--------------------------------------- */
function DblFilter2(e)
{          /*Grabbing the unicode value of the key that was pressed*/
	 var unicode;
    if(window.event)
          unicode = window.event.keyCode;     //IE
    else
      unicode = e.which;     //firefox
      /* tab null backspace, minus*/
      if(unicode == 9 || unicode == 8 || unicode == 0 || unicode == 45 || unicode == 189)
        {
            return true;
        }
     /*alert(unicode);*/
    /*if the value entered is not a unicode value between 48 and 57 return false - permit [.] */
    
    if(unicode == 46)
    {
        return true;
    }
    else
    {
        if(unicode < 48 || unicode > 57)
        {  
            return false;
        } 
    }
    return true;
}
/*------End kiểm tra số nguyên--------------------------------------- */

/*------Kiểm tra phím--------------------------------------- */
var captureKeys = function(ev) 
{
	ev = ev || window.event;             // gets the event in ie or ns
	kCode = ev.keyCode || ev.which;   // gets the keycode in ie or ns
 
	/* in ie, when pressing the ctrl + shift + key, it gives the key code for the capitalized key (probably because shift is pressed) 
	   in ns pressing ctrl, shift and another key doesn't change the keycode
	   thus, the || and two different numbers */
// alert("123 - "+ kCode + "-" + ev);
	if (ev.ctrlKey && ev.shiftKey && kCode == 19 || ev.ctrlKey && ev.shiftKey && kCode == 83) 
	{    // ctrl+alt+s
				// another function that does something
	   return false;  // make it so the browser ignores key combo
   }
   if (ev.ctrlKey && kCode == 119) 
   { // ctrl+w
	   //alert("456");
	   closeWin();  // run your own script to close the window 
							   // doesn't work in ie, ie just closes the window
   }
   if ( (ev.ctrlKey && kCode == 10) || (ev.ctrlKey && kCode == 13))//enter firefox: enter is 13
   { // ctrl+w
	   var btn = document.getElementById("Button1P92");  // run your own script to close the window 
							   // doesn't work in ie, ie just closes the window
	   btn.onclick();
		  return false;
   }
}

 /* -------- Định dạng chuỗi của text   ----------*/
function gcFormat(strId)
{
    var txtbox = document.getElementById(strId);
    var index = getCaretStart(txtbox);
    var lB = txtbox.value.length;
    txtbox.value = gcNum(txtbox.value, ",", ".");
    var lE = txtbox.value.length;
    var newindex = index + (lE-lB);
    if(newindex<0)
        newindex = 0;
    setCaret(txtbox, newindex);
}

 /* -------- Chuyển định dạng  ----------*/
function gcRev( str, t1)
{
   
    var idx = str.indexOf( t1 );
    while ( idx > -1 ) {
        str = str.replace( t1, "" );
        idx = str.indexOf( t1 );
    }
    return str;
}
//-----------------------------------------------
function ltrim(str) { 
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
	return str.substring(k, str.length);
}
function rtrim(str) {
	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
	return str.substring(0,j+1);
}
function trim(str) {
	return ltrim(rtrim(str));
}
function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}
//-----------------------------------------------


 /* -------- Chuyển định dạng ngay Viet -> Anh  ----------*/
function gcDateC( str )
{
    if(str==null)
        return str;
    if(str.length<4)
        return "";
    var arrString = str.split( '/' );
    if(arrString.length!=3)
        return "";
    var str = arrString[1]+'/'+arrString[0]+'/'+arrString[2];
    /*testing */
    var month = parseInt(arrString[1], 10);
    var day = parseInt(arrString[0], 10);
    var year = parseInt(arrString[2], 10);
    if(month<1 || month>12)
        str = "";
    if(day<1 || day>31)
        str = "";
    if(year<1900 || year>2999)
        str = "";
    return str;
}
/*------------------------ change format ---------------------------------- */
/* t1=[,] t2=[.] sử dụng theo số tiếng anh gcNum(str,",",".")*/
function gcNum( str, t1, t2 )
{
    var tmpValue = str;
    var arrString = new Array(Math.round(tmpValue.length/3) + 2);

    tmpValue = tmpValue.replace(t1,"");

    /* -------- Xử lý các dấu chấm   ----------*/
    var idx = tmpValue.indexOf( t1 );
    while ( idx > -1 ) {
        tmpValue = tmpValue.replace( t1, "" );
        idx = tmpValue.indexOf( t1 );
    }
    /*-----------------------------------------*/

    var idxMinus = tmpValue.indexOf( "-" );
    if(idxMinus==0)
        tmpValue = tmpValue.substring(idxMinus+1, tmpValue.length); 
    else  if(idxMinus>0)
        tmpValue = tmpValue.substring(0, idxMinus); 
    /*/-----------------remove all dấu [-]-------------------*/
    /* -------- Xử lý các dấu chấm   ----------*/
    var idxM = tmpValue.indexOf( "-" );
    while ( idxM > -1 ) {
        tmpValue = tmpValue.replace( "-", "" );
        idxM = tmpValue.indexOf( "-" );
    }
    /*----------------------------------------*/
     var k = 0;
     var i = 0;
    idx = tmpValue.indexOf( t2 );
    var strTail = "";
    if(idx>=0)
    {
        var strHead = tmpValue.substring(0, idx); 
        
        var lH = strHead.length;
        if(lH>3)
        {
            for (k = lH - 3; k > 0; k = k - 3)
            {
                arrString[i] = strHead.substring(k,k+3);
                i++;
            }
            /* ------------------------------------ */
            if(k<=0)
            {
                arrString[i] = strHead.substring(0,k+3);
                i++;
               }
        }/* end lH */
        else
            {
                arrString[i] = strHead.substring(0,strHead.length);
                i++;
            }
        
        var strTail = tmpValue.substring(idx+1, tmpValue.length); 
        /* remove all comma (,) */
        var idx2 = strTail.indexOf( t2 );
        while ( idx2 > -1 ) {
            strTail = strTail.replace( t2, "" );
            idx2 = strTail.indexOf( t2 );
        }
     }
     else
     {
        var strHead = tmpValue; 
        var lH = strHead.length;
        if(lH>3)
        {
            for (k = lH - 3; k > 0; k = k - 3)
            {
                arrString[i] = strHead.substring(k,k+3);
                i++;
            }
            /* ------------------------------------ */
            if(k<=0)
            {
                arrString[i] = strHead.substring(0,k+3);
                i++;
            }
        }/* end lH */
        else
            {
                arrString[i] = strHead.substring(0,strHead.length);
                i++;
            }
     }              

    var j=0; tmpValue = "";
    if(idxMinus>=0)
        tmpValue += "-";
        
    /* concat Head */
    for(j=i-1; j>0; j--)
    {
        tmpValue+= arrString[j];
        if(j>0)
            tmpValue+= t1;
    }
    if(i>0)
        tmpValue+= arrString[0];

    /* concar Tail */
    if(idx>=0)
    {
        tmpValue+= t2;
        tmpValue+= strTail;
    }
    return tmpValue;
}
/*    Caret Functions     */

// Get the end position of the caret in the object. Note that the obj needs to be in focus first
function getCaretEnd(obj){
	if(typeof obj.selectionEnd != "undefined"){
		return obj.selectionEnd;
	}else if(document.selection&&document.selection.createRange){
		var M=document.selection.createRange();
		try{
			var Lp = M.duplicate();
			Lp.moveToElementText(obj);
		}catch(e){
			var Lp=obj.createTextRange();
		}
		Lp.setEndPoint("EndToEnd",M);
		var rb=Lp.text.length;
		if(rb>obj.value.length){
			return -1;
		}
		return rb;
	}
}
// Get the start position of the caret in the object
function getCaretStart(obj){
	if(typeof obj.selectionStart != "undefined"){
		return obj.selectionStart;
	}else if(document.selection&&document.selection.createRange){
		var M=document.selection.createRange();
		try{
			var Lp = M.duplicate();
			Lp.moveToElementText(obj);
		}catch(e){
			var Lp=obj.createTextRange();
		}
		Lp.setEndPoint("EndToStart",M);
		var rb=Lp.text.length;
		if(rb>obj.value.length){
			return -1;
		}
		return rb;
	}
}
// sets the caret position to l in the object
function setCaret(obj,l){
	obj.focus();
	if (obj.setSelectionRange){
		obj.setSelectionRange(l,l);
	}else if(obj.createTextRange){
		m = obj.createTextRange();		
		m.moveStart('character',l);
		m.collapse();
		m.select();
	}
}
// sets the caret selection from s to e in the object
function setSelection(obj,s,e){
	obj.focus();
	if (obj.setSelectionRange){
		obj.setSelectionRange(s,e);
	}else if(obj.createTextRange){
		m = obj.createTextRange();		
		m.moveStart('character',s);
		m.moveEnd('character',e);
		m.select();
	}
}


function GridCreated(sender, eventArgs)
 {
     var gridElement = sender.get_element();
     var elementsToUse = [];
     inputs = gridElement.getElementsByTagName("input");
     for (var i = 0; i < inputs.length;i++)
     {
     if(inputs[i].type.toLowerCase() == "hidden")
     {
                continue;
     }
        
     Array.add(elementsToUse, inputs[i]);
         inputs[i].onchange = TrackChanges;
     }
        
     dropdowns = gridElement.getElementsByTagName("select");
     for (var i = 0; i < dropdowns.length;i++)
     {
         dropdowns[i].onchange = TrackChanges;
     }

 }
 
 function TrackChanges(e)
 {
     hasChanges = true;
 }
  function focusOn(sender, args)
 {
       var textBox = sender;
       textBox.focus();
 }
 


 